package com.jianyun.jyzs.dao;

import android.content.Context;
import android.text.TextUtils;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.db.DatabaseHelper;
import com.jianyun.jyzs.utils.TimeUtils;


import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/8/16.
 */

public class CrmDao {
    private Context context;
    private Dao<CrmDetial.CrmDetailBean, Integer> crmDao;
    private DatabaseHelper helper;


    public CrmDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            crmDao = helper.getDao(CrmDetial.CrmDetailBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insertCrm(CrmDetial.CrmDetailBean poBean) {
        try {
//            String userId = CacheTask.getInstance().getUserId();
//            poBean.setTargetUser(userId);
            crmDao.createOrUpdate(poBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<CrmDetial.CrmDetailBean> getCrmList(String userid, String en) {
        List<CrmDetial.CrmDetailBean> list = new ArrayList<>();
        QueryBuilder<CrmDetial.CrmDetailBean, Integer> builder = crmDao.queryBuilder().orderBy("createDt",false);
        try {
            Where<CrmDetial.CrmDetailBean, Integer> where = builder.where().eq("enterpriseCode", en).and().eq("createUserId",userid);
            list = where.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    public void delCrm(String id) {
        try {
            crmDao.deleteById(Integer.parseInt(id));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<CrmDetial.CrmDetailBean> searchCondition(String userId, String enterpriseCode, List<String> list_state, List<String> list_instudry) {
        List<CrmDetial.CrmDetailBean> list = new ArrayList<>();
        Where<CrmDetial.CrmDetailBean, Integer> where = crmDao.queryBuilder().orderBy("id", false).where();

        try {


            for (String s:list_state
                 ) {
                where.clear();
                where.eq("enterpriseCode",enterpriseCode).and().eq("createUserId",userId);
                list.addAll(where.and().eq("funnel",s).query());
            }
            for (String s:list_instudry) {
                where.clear();
                where.eq("enterpriseCode",enterpriseCode).and().eq("createUserId",userId);
                list.addAll(where.and().eq("property",s).query());
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        }

    }

    public List<CrmDetial.CrmDetailBean> searchTime(String enterpriseCode, String userId, boolean b) {
        List<CrmDetial.CrmDetailBean> list = new ArrayList<>();
        QueryBuilder<CrmDetial.CrmDetailBean, Integer> builder = crmDao.queryBuilder().orderBy("createDt", b);
        Where<CrmDetial.CrmDetailBean, Integer> where = builder.where();
        try {
            where.eq("enterpriseCode", enterpriseCode).and().eq("createUserId", userId);
            list = where.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    public List<CrmDetial.CrmDetailBean> search(String enterpriseCode, String userId, String mc, String dz, String zt, String jl) {
        List<CrmDetial.CrmDetailBean> list = new ArrayList<>();
        QueryBuilder<CrmDetial.CrmDetailBean, Integer> builder = crmDao.queryBuilder();
        Where<CrmDetial.CrmDetailBean, Integer> where = builder.where();
        try {
            where.eq("enterpriseCode", enterpriseCode).and().eq("createUserId", userId);
            if (!TextUtils.isEmpty(mc)) where.and().like("name", "%" + mc + "%");
            if (!TextUtils.isEmpty(dz)) where.and().like("address", "%" + dz + "%");
            if (!TextUtils.isEmpty(zt)) where.and().eq("funnel", zt);
            list = where.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    //根据时间搜索CRM
    public List<CrmDetial.CrmDetailBean> getCalendarCrm(String userId, String enterpriseCode, String s, int tagIndex) {
        RceLog.d("lml", "时间搜索" + s);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<CrmDetial.CrmDetailBean> list = new ArrayList<>();
        QueryBuilder<CrmDetial.CrmDetailBean, Integer> builder1 = crmDao.queryBuilder();
        QueryBuilder<CrmDetial.CrmDetailBean, Integer> builder2 = crmDao.queryBuilder();
        Where<CrmDetial.CrmDetailBean, Integer> w1 = builder1.where();
        Where<CrmDetial.CrmDetailBean, Integer> w2 = builder2.where();
        try {
            long time = TimeUtils.getCurrentLong();
            w1.eq("enterpriseCode", enterpriseCode).and().eq("createUserId", userId).and().eq("nextPhoneDt", s);
            w2.eq("enterpriseCode", enterpriseCode).and().eq("createUserId", userId).and().eq("nextVisitDt", s);

            List<CrmDetial.CrmDetailBean> query1 = w1.query();
            RceLog.d("lml", query1.toString());
            List<CrmDetial.CrmDetailBean> query2 = w2.query();
            RceLog.d("lml", query2.toString());
            for (CrmDetial.CrmDetailBean cp : query1) {
                if (tagIndex == 0) {
                    long ptime = sdf.parse(cp.getNextPhoneDt()).getTime();
                    RceLog.d("lml", "Index = 0 getNextPhoneDt" + ptime);
                    if (ptime > time) {
                        cp.setNextTag(0);
                        list.add(cp);
                    }
                }
                if (tagIndex == 1) {
                    long ptime = sdf.parse(cp.getNextPhoneDt()).getTime();
                    RceLog.d("lml", "Index = 1 getNextPhoneDt" + ptime);
                    if (ptime < time) {
                        cp.setNextTag(0);
                        list.add(cp);
                    }
                }
            }
            for (CrmDetial.CrmDetailBean cp : query2) {
                if (tagIndex == 0) {
                    long ptime = sdf.parse(cp.getNextVisitDt()).getTime();
                    RceLog.d("lml", "Index = 0 getNextVisitDt" + ptime);
                    if (ptime > time) {
                        cp.setNextTag(1);
                        list.add(cp);
                    }
                }
                if (tagIndex == 1) {
                    long ptime = sdf.parse(cp.getNextVisitDt()).getTime();
                    RceLog.d("lml", "Index = 1 getNextVisitDt" + ptime);
                    if (ptime < time) {
                        cp.setNextTag(1);
                        list.add(cp);
                    }
                }
            }
            RceLog.d("lml", "dao return___" + list.toString());
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        }

    }

    public List<CrmDetial.CrmDetailBean> searchAll() {
        List<CrmDetial.CrmDetailBean> list = new ArrayList<>();
        QueryBuilder<CrmDetial.CrmDetailBean, Integer> builder = crmDao.queryBuilder();
        try {
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
